package com.example.elm.Home_Page;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.elm.First_Page.First_Page;
import com.example.elm.R;
import com.example.elm.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Choose_Pay_Method_Page extends AppCompatActivity {

    TextView product_price, min_text_view, second_text_view;
    CheckBox choose_use_bank_card_pay, choose_use_alipay, choose_use_wechat_pay;
    Button pay_button;

    AlertDialog dialog;
    String which_pay_method = "支付宝支付";
    int second = 0, min = 15;
    boolean is_upload_order = false;
    String price, shop_name, order_address, shop_email, user_email, user_name, user_sex, user_phone_number, note, table_ware, product_data, packing_fee, delivery_fee, this_order_has_how_many_discount;
    double how_many_km;
    int how_many_product, all_product_num;

    private class Min_And_Second_Thread extends Thread {
        @Override
        public void run() {
            while (min != 0 || second != 0) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (second == 0) {
                            min -= 1;
                            second = 59;
                        }
                        else {
                            second -= 1;
                        }

                        if (min < 10) {
                            min_text_view.setText("0" + min);
                        }
                        else {
                            min_text_view.setText("" + min);
                        }

                        if (second < 10) {
                            second_text_view.setText("0" + second);
                        }
                        else {
                            second_text_view.setText("" + second);
                        }
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private class Finish_Pay_Thread extends Thread {
        @Override
        public void run() {
            is_upload_order = false;

            ExecutorService executorService1 = Executors.newSingleThreadExecutor();
            executorService1.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    PreparedStatement preparedStatement;
                    String sql = "INSERT INTO `elm`.`" + shop_email + "_Merchant_Order`" +
                            "(order_number, user_name, user_phone_number, time_to_prepare_meals, user_address, rider_is_get_order, rider_name, rider_get_order_time, all_product, how_many_product, product_data, packing_fee, order_state, tableware, order_time, delivery_fee, actual_payment, discount_how_many, how_many_km, note) " +
                            "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, "");
                    preparedStatement.setString(2, user_name);
                    preparedStatement.setString(3, user_phone_number);
                    preparedStatement.setString(4, "");
                    preparedStatement.setString(5, order_address);
                    preparedStatement.setInt(6, 0);
                    preparedStatement.setString(7, "");
                    preparedStatement.setString(8, "");
                    preparedStatement.setInt(9, all_product_num);
                    preparedStatement.setInt(10, how_many_product);
                    preparedStatement.setString(11, product_data);
                    preparedStatement.setDouble(12, Double.parseDouble(packing_fee));
                    preparedStatement.setInt(13, 0);
                    if (table_ware.equals("已选无需餐具")) {
                        preparedStatement.setInt(14, 0);
                    }
                    else if (table_ware.equals("按餐量提供")) {
                        preparedStatement.setInt(14, -1);
                    }
                    else {
                        preparedStatement.setInt(14, Integer.parseInt(table_ware.substring(0, table_ware.length() - 2)));
                    }
                    preparedStatement.setString(15, "");
                    if (delivery_fee.equals("免配送费")) {
                        preparedStatement.setDouble(16, 0);
                    }
                    else {
                        preparedStatement.setDouble(16, Double.parseDouble(delivery_fee));
                    }
                    preparedStatement.setDouble(17, Double.parseDouble(price));
                    preparedStatement.setDouble(18, Double.parseDouble(this_order_has_how_many_discount));
                    preparedStatement.setDouble(19, how_many_km);
                    preparedStatement.setString(20, note);
                    preparedStatement.executeUpdate();

                    String sql2 = "INSERT INTO `elm`.`" + user_email + "_user_order`" +
                            "(shop_name, order_state, order_address, tableware, delivery_rider_name, is_phone_number_protection, order_number, order_time, payment_methods, product_data, packing_fee, delivery_fee, use_red_envelope, actual_payment, shop_email, discount_how_many, how_many_km, note) " +
                            "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
                    preparedStatement = connection.prepareStatement(sql2);
                    preparedStatement.setString(1, shop_name);
                    preparedStatement.setInt(2, 0);
                    preparedStatement.setString(3, order_address);
                    if (table_ware.equals("已选无需餐具")) {
                        preparedStatement.setInt(4, 0);
                    }
                    else if (table_ware.equals("按餐量提供")) {
                        preparedStatement.setInt(4, -1);
                    }
                    else {
                        preparedStatement.setInt(4, Integer.parseInt(table_ware.substring(0, table_ware.length() - 2)));
                    }
                    preparedStatement.setString(5, "");
                    preparedStatement.setInt(6, 0);
                    preparedStatement.setString(7, "");
                    preparedStatement.setString(8, "");
                    preparedStatement.setInt(9, 0);
                    preparedStatement.setString(10, product_data);
                    preparedStatement.setDouble(11, Double.parseDouble(packing_fee));
                    if (delivery_fee.equals("免配送费")) {
                        preparedStatement.setDouble(12, 0);
                    }
                    else {
                        preparedStatement.setDouble(12, Double.parseDouble(delivery_fee));
                    }
                    preparedStatement.setString(13, "");
                    preparedStatement.setDouble(14, Double.parseDouble(price));
                    preparedStatement.setString(15, shop_email);
                    preparedStatement.setDouble(16, Double.parseDouble(this_order_has_how_many_discount));
                    preparedStatement.setDouble(17, how_many_km);
                    preparedStatement.setString(18, note);
                    preparedStatement.executeUpdate();
                    is_upload_order = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }

                while (!is_upload_order) {
                    try {
                        Thread.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                        finish();
                    }
                });
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_pay_method_page);

        price = getIntent().getStringExtra("price");
        shop_name = getIntent().getStringExtra("shop_name");
        order_address = getIntent().getStringExtra("order_address");
        shop_email = getIntent().getStringExtra("shop_email");
        user_email = getIntent().getStringExtra("user_email");
        user_name = getIntent().getStringExtra("user_name");
        user_sex = getIntent().getStringExtra("user_sex");
        user_phone_number = getIntent().getStringExtra("user_phone_number");
        how_many_km = getIntent().getDoubleExtra("how_many_km", 0);
        note = getIntent().getStringExtra("note");
        table_ware = getIntent().getStringExtra("table_ware");
        how_many_product = getIntent().getIntExtra("how_many_product", 0);
        all_product_num = getIntent().getIntExtra("all_product_num", 0);
        product_data = getIntent().getStringExtra("product_data");
        packing_fee = getIntent().getStringExtra("packing_fee");
        delivery_fee = getIntent().getStringExtra("delivery_fee");
        this_order_has_how_many_discount = getIntent().getStringExtra("this_order_has_how_many_discount");

        product_price = findViewById(R.id.product_price);
        min_text_view = findViewById(R.id.min_text_view);
        second_text_view = findViewById(R.id.second_text_view);
        choose_use_bank_card_pay = findViewById(R.id.choose_use_bank_card_pay);
        choose_use_alipay = findViewById(R.id.choose_use_alipay);
        choose_use_wechat_pay = findViewById(R.id.choose_use_wechat_pay);
        pay_button = findViewById(R.id.pay_button);

        product_price.setText(price);

        Min_And_Second_Thread minAndSecondThread = new Min_And_Second_Thread();
        minAndSecondThread.start();

        choose_which_pay_method();

        pay_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();
                Finish_Pay_Thread finishPayThread = new Finish_Pay_Thread();
                finishPayThread.start();
            }
        });
    }

    public void choose_which_pay_method() {
        choose_use_bank_card_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choose_use_alipay.setChecked(false);
                choose_use_wechat_pay.setChecked(false);
                which_pay_method = "银行卡支付";
            }
        });

        choose_use_alipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choose_use_bank_card_pay.setChecked(false);
                choose_use_wechat_pay.setChecked(false);
                which_pay_method = "支付宝支付";
            }
        });

        choose_use_wechat_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choose_use_alipay.setChecked(false);
                choose_use_bank_card_pay.setChecked(false);
                which_pay_method = "微信支付";
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}